package com.example.tired1.zyfypt_14120200131_syn.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.tired1.zyfypt_14120200131_syn.R;
import com.example.tired1.zyfypt_14120200131_syn.bean.Constant;
import com.example.tired1.zyfypt_14120200131_syn.bean.UserBean;
import com.example.tired1.zyfypt_14120200131_syn.iface.LoginLister;
import com.example.tired1.zyfypt_14120200131_syn.model.LoginMode;


public class LoginActivity extends AppCompatActivity implements LoginLister,View.OnClickListener{
   EditText et_username,et_password;
    Button btn_login,btn_register;
    CheckBox checkBox;
    Context context;
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        context=LoginActivity.this;

        preferences=getSharedPreferences(Constant.PREM_NAME,MODE_PRIVATE);//初始化
        boolean isChecked=preferences.getBoolean("isChecked",false);
        checkBox.setChecked(isChecked);
        if(isChecked){
            String username=preferences.getString("username","");
            String password=preferences.getString("password","");
            et_username.setText(username);
            et_password.setText(password);


        }


    }

    private void initView() {
        et_username=(EditText)findViewById(R.id.et_username);
        et_password=(EditText)findViewById(R.id.et_password);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_register=(Button)findViewById(R.id.btn_register);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }
    @Override
    public void onResponse(UserBean userBean) {
            Toast.makeText(context, "登录成功",
                    Toast.LENGTH_SHORT).show();


        preferences=getSharedPreferences(Constant.PREM_NAME,MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();

        boolean inChecked=checkBox.isChecked();
        editor.putBoolean("isChecked",inChecked);
        editor.putString("sessionId",userBean.getSessionid());
        if(inChecked){//保存当前状态信息
            String username=et_username.getText().toString();//获取
            String password=et_password.getText().toString();
            editor.putString("username",username);
            editor.putString("password",password);

        }
        editor.commit();

           Intent intent=new Intent(context,MainActivity.class);//发起跳转的一方
            startActivity(intent);
            finish();//发出意图
        }
    @Override
    public void onFail(String s) {
        Toast.makeText(context,"s",
                Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id. btn_login:
                String username=et_username.getText().toString();//获取用户信息
                String password=et_password.getText().toString();
                LoginMode mode=new LoginMode();
                mode.login(username,password,this);//this：监听器  发出网络登录请求
                break;
            case R.id.btn_register:
                Intent intent=new Intent(context,RegisterActivity.class);//发起跳转的一方
                startActivity(intent);//发出意图
                finish();//关掉注册的界面
                break;
    }
}}
